package com.zz.utils;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

@Slf4j
@UtilityClass
public class PropertiesUtil {

    /**
     * 返回resources下的全路径
     *
     * @param fileName resources下的文件名，如果resources还有其他目录，要加上
     * @return 全路径
     */
    public static String getResourcesPath(String fileName) {
        String dir = System.getProperty("user.dir");
        return dir + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + fileName;

    }

    /**
     * 在指定properties文件中，通过【key】匹配，有，则返回相应的value
     *
     * @param keyWord        要查询的关键字
     * @param searchFileName 要搜索的文件名称
     * @return 返回相应的value
     */
    public String searchKeyWordByKey(String keyWord, String searchFileName) {
        Properties properties = new Properties();
        ClassPathResource classPathResource = new ClassPathResource(searchFileName);
        InputStream inputStream;
        try {
            inputStream = classPathResource.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            properties.load(bufferedReader);
            Set<Object> keySets = properties.keySet();
            for (Object obj : keySets) {
                String key = (String) obj;
                String value = (String) properties.get(obj);
                //System.out.println("键 = " + key);
                //System.out.println("值 = " + value);
                if (keyWord.equalsIgnoreCase(key)) {
                    return value;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 在指定properties文件中，通过【value】匹配，有，则返回相应的key
     *
     * @param keyWord        要查询的关键字
     * @param searchFileName 要搜索的文件名称
     * @return 返回相应的key
     */
    public String searchKeyWordByValue(String keyWord, String searchFileName) {
        Properties properties = new Properties();
        ClassPathResource classPathResource = new ClassPathResource(searchFileName);
        InputStream inputStream;
        try {
            inputStream = classPathResource.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            properties.load(bufferedReader);
            Set<Object> keySets = properties.keySet();
            for (Object obj : keySets) {
                String key = (String) obj;
                String value = (String) properties.get(obj);
                System.out.println("键 = " + key);
                System.out.println("值 = " + value);
                if (keyWord.equalsIgnoreCase(value)) {
                    return key;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 向指定的properties文件中，【追加】写入数据
     *
     * @param fileName 要写入的文件名称
     * @param map      要写入的数据
     */
    public static void writeToProperties(String fileName, Map<String, String> map) {
        Properties properties = new Properties();
        String path = getResourcesPath(fileName);
        try {
            // true:追加，不设为true，则是重新输入
            FileOutputStream fos = new FileOutputStream(path, true);
            OutputStreamWriter opw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);

            Set<String> set = map.keySet();
            for (String key : set) {
                String value = map.get(key);
                properties.setProperty(key, value);
            }
            properties.store(opw, null);
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 在指定的properties文件中，修改数据
     *
     * @param fileName 要修改的文件名称
     * @param map      要修改的数据
     */
    public static void updateProperties(String fileName, Map<String, String> map) {
        Properties properties = new Properties();
        String path = getResourcesPath(fileName);

        InputStreamReader reader;
        OutputStreamWriter writer;
        try {
            reader = new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8);
            properties.load(reader);
            writer = new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8);
            for (String key : map.keySet()) {
                properties.setProperty(key, map.get(key));
            }
            properties.store(writer, null);
            reader.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除指定的key
     * @param fileName 文件名
     * @param key 要删除的key
     */
    public static void removeProperty(String fileName, String key) {
        Properties properties = new Properties();
        String path = getResourcesPath(fileName);

        InputStreamReader reader;
        OutputStreamWriter writer;
        try {
            reader = new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8);
            properties.load(reader);
            writer = new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8);
            properties.remove(key);
            properties.store(writer, null);
            reader.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}